package com.controller.config;

import com.common.anntation.Log;
import com.common.emums.BusinessType;
import com.common.utils.DateUtils;
import com.common.utils.PythonUtil;
import com.model.base.Result;
import com.model.domain.data.GetDataRepairUserAlgorithmInfoDO;
import com.model.dto.config.GetOneDTO;
import com.model.dto.order.task.GetOrderTaskInfoListDTO;
import com.model.pojo.data.DataRepairUserAlgorithmInfo;
import com.model.vo.config.GetMaintainerInfoVO;
import com.model.vo.config.GetOneVO;
import com.service.data.DataRepairUserAlgorithmInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @className: ConfigMaintainerController
 * @description: TODO 类描述
 * @author: zql
 * @date: 2023/10/22 23:10
 * @Company: Copyright© [日期] by [作者或个人]
 **/
@Api(tags = {"维修人员管理"})
@Slf4j
@RestController
@RequestMapping("/v1/config/maintainer")
@RequiredArgsConstructor
public class ConfigMaintainerController {
    private final DataRepairUserAlgorithmInfoService dataRepairUserAlgorithmInfoService;

    /**
     * @Author: zql
     * @Description: 维修人员管理列表
     * @param dto 请求数据
     * @Data: 2023-10-22
     */
    @Log(title = "维修人员管理-维修人员管理列表", businessType = BusinessType.SELECT,  isSaveResponseData = false)
    @ApiOperation(value = "维修人员管理列表")
    @PostMapping("/getMaintainerList")
    public Result<List<GetMaintainerInfoVO>> getMaintainerList(@RequestBody @Valid GetOrderTaskInfoListDTO dto) {
        List<GetMaintainerInfoVO> getMaintainerInfoVOList = new ArrayList<>();
        Date date = DateUtils.parseStrToDate(dto.getDate(),DateUtils.DATE_FORMAT_YYYY_MM_DD);
        Date firstDate = DateUtils.getFirstDayOfMonth(Integer.valueOf(DateUtils.getYear(date)),Integer.valueOf(DateUtils.getMonth(date)));
        String sTime = DateUtils.parseDateToStr(DateUtils.addDays(firstDate,-1),DateUtils.DATE_FORMAT_YYYY_MM_DD);
        String eTime = DateUtils.parseDateToStr(DateUtils.addDays(date,1),DateUtils.DATE_FORMAT_YYYY_MM_DD);
        GetDataRepairUserAlgorithmInfoDO getDataRepairUserAlgorithmInfoDO = new GetDataRepairUserAlgorithmInfoDO();
        getDataRepairUserAlgorithmInfoDO.setSortRow("id");
        getDataRepairUserAlgorithmInfoDO.setSortOrder("asc");
        String provice = "河南省";
        String city = "周口市";
        String county = "川汇区";
        if(dto.getProvince()!=null && !"".equals(dto.getProvince()))provice = dto.getProvince();
        if(dto.getCity()!=null && !"".equals(dto.getCity()))city = dto.getCity();
        if(dto.getCounty()!=null && !"".equals(dto.getCounty())) county = dto.getCounty();
        getDataRepairUserAlgorithmInfoDO.setProvinceName(provice);
        getDataRepairUserAlgorithmInfoDO.setCityName(city);
//        getDataRepairUserAlgorithmInfoDO.setCountyName(county);
        getDataRepairUserAlgorithmInfoDO.setSTime(sTime);
        getDataRepairUserAlgorithmInfoDO.setETime(eTime);
        List<DataRepairUserAlgorithmInfo> list = dataRepairUserAlgorithmInfoService.getAllList(getDataRepairUserAlgorithmInfoDO);
        if(list!=null && list.size()>0){
            for(int i = 0;i<list.size();i++){
                DataRepairUserAlgorithmInfo info = list.get(i);
                GetMaintainerInfoVO vo = new GetMaintainerInfoVO();
                vo.setMaintainerId(info.getId());
                vo.setProvince(info.getProvinceName());
                vo.setCity(info.getCityName());
                vo.setArea(info.getCountyName());
                vo.setAreaCode(info.getQuhao());
                vo.setWorkDate(DateUtils.parseDateToStr(DateUtils.DATE_FORMAT_YYYY_MM_DD,info.getWorkDay()));
                vo.setCarTotal(info.getGpsMachCountOneDay());//车辆总数
                vo.setSpanAreaCarTotal(info.getGpsMachCrNumOneDay());//跨区车辆总数
                vo.setSpanAreaOrderTotal(info.getOrderCrOrderOneDay());//跨区服务订单总数
                vo.setSpanAreaPeopleTotal("0");//跨区服务人员总数
                String spanAreaOrderTotal = "0";
                String spanAreaPeopleTotal = "0";
                DataRepairUserAlgorithmInfo info1 = null;
                DataRepairUserAlgorithmInfo info2 = null;
                DataRepairUserAlgorithmInfo info3 = null;
                if(i==1){
                    info1 = list.get(0);
                }else if(i == 2){
                    info1 = list.get(1);
                    info2 = list.get(0);
                }else if(i>3){
                    info1 = list.get(i-1);
                    info2 = list.get(i-2);
                    info3 = list.get(i-3);
                }
                int month = Integer.parseInt(DateUtils.getMonth(info.getWorkDay()));
                int day = Integer.parseInt(DateUtils.getDate(info.getWorkDay()));
                String quhao = info.getQuhao();
                BigDecimal gps_mach_count_one_day = BigDecimal.ZERO;
                BigDecimal gps_mach_cr_num_one_day = BigDecimal.ZERO ;
                BigDecimal man_local_num_one_day = BigDecimal.ZERO;
                if(info1!=null){
                    gps_mach_count_one_day = gps_mach_count_one_day.add(new BigDecimal(info1.getGpsMachCountOneDay()));
                    gps_mach_cr_num_one_day = gps_mach_cr_num_one_day.add(new BigDecimal(info1.getGpsMachCrNumOneDay()));
                }
                if(info2!=null){
                    gps_mach_count_one_day = gps_mach_count_one_day.add(new BigDecimal(info2.getGpsMachCountOneDay()));
                    gps_mach_cr_num_one_day = gps_mach_cr_num_one_day.add(new BigDecimal(info2.getGpsMachCrNumOneDay()));
                }
                if(info3!=null){
                    gps_mach_count_one_day = gps_mach_count_one_day.add(new BigDecimal(info3.getGpsMachCountOneDay()));
                    gps_mach_cr_num_one_day = gps_mach_cr_num_one_day.add(new BigDecimal(info3.getGpsMachCrNumOneDay()));
                }
                String params = month+" "+day+" "+quhao+" "+gps_mach_count_one_day.doubleValue()+" "+gps_mach_cr_num_one_day.doubleValue()+" "+man_local_num_one_day;
                List<String> pyList = PythonUtil.callRepairUser(params);
                if(pyList!=null && pyList.size()>1){
                    spanAreaOrderTotal = pyList.get(0);
                    spanAreaPeopleTotal = pyList.get(1);
                }
                if(spanAreaOrderTotal.lastIndexOf(".") == spanAreaOrderTotal.length()-1)spanAreaOrderTotal = spanAreaOrderTotal.substring(0,spanAreaOrderTotal.length()-1);
                if(spanAreaPeopleTotal.lastIndexOf(".") == spanAreaPeopleTotal.length()-1)spanAreaPeopleTotal = spanAreaPeopleTotal.substring(0,spanAreaPeopleTotal.length()-1);
                vo.setSpanAreaCarTotalPredict(spanAreaOrderTotal);//跨区服务订单总数预测值
                vo.setSpanAreaPeopleTotalPredict(spanAreaPeopleTotal);//跨区服务人员总数预测值
                vo.setCreatedTime(new Date());
                getMaintainerInfoVOList.add(vo);
            }
        }
        return Result.suc(getMaintainerInfoVOList);
    }



    /**
     * @Author: zql
     * @Description: 单预测按钮
     * @param dto 请求数据
     * @Data: 2023-10-22
     */
    @Log(title = "维修人员管理-单预测按钮", businessType = BusinessType.SELECT,  isSaveResponseData = false)
    @ApiOperation(value = "单预测按钮")
    @PostMapping("/getOnePredict")
    public Result<List<GetOneVO>> getOnePredict(@RequestBody @Valid GetOneDTO dto) {
        List<GetOneVO> vo = new ArrayList<GetOneVO>();
        String spanAreaOrderTotal = "0";
        String spanAreaPeopleTotal = "0";
        Date date = DateUtils.parseStrToDate(dto.getDate(),DateUtils.DATE_FORMAT_YYYY_MM_DD);
        GetDataRepairUserAlgorithmInfoDO getDataRepairUserAlgorithmInfoDO = new GetDataRepairUserAlgorithmInfoDO();
        getDataRepairUserAlgorithmInfoDO.setSortRow("id");
        getDataRepairUserAlgorithmInfoDO.setSortOrder("asc");
        getDataRepairUserAlgorithmInfoDO.setProvinceName(dto.getProvince());
        getDataRepairUserAlgorithmInfoDO.setCityName(dto.getCity());
        getDataRepairUserAlgorithmInfoDO.setCountyName(dto.getCounty());
        Date startTime = DateUtils.parseStrToDate(DateUtils.parseDateToStr(date,DateUtils.DATE_FORMAT_YYYY_MM_DD)+" 00:00:00",DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        Date endTime = DateUtils.parseStrToDate(DateUtils.parseDateToStr(date,DateUtils.DATE_FORMAT_YYYY_MM_DD)+" 23:59:59",DateUtils.DATE_TIME_FORMAT_YYYY_MM_DD_HH_MI_SS);
        getDataRepairUserAlgorithmInfoDO.setTime(dto.getDate());
        List<DataRepairUserAlgorithmInfo> list = dataRepairUserAlgorithmInfoService.getAllList(getDataRepairUserAlgorithmInfoDO);
        if(list!=null && list.size()>0){
            DataRepairUserAlgorithmInfo info = list.get(0);
            getDataRepairUserAlgorithmInfoDO.setSTime(DateUtils.parseDateToStr(DateUtils.addDays(startTime,-4),DateUtils.DATE_FORMAT_YYYY_MM_DD));
            getDataRepairUserAlgorithmInfoDO.setETime(dto.getDate());
            getDataRepairUserAlgorithmInfoDO.setTime(null);
            list = dataRepairUserAlgorithmInfoService.getAllList(getDataRepairUserAlgorithmInfoDO);
            if(list!=null && list.size()>0){
                int month = Integer.parseInt(DateUtils.getMonth(date));
                int day = Integer.parseInt(DateUtils.getDate(date));
                String quhao = info.getQuhao();
                BigDecimal gps_mach_count_one_day = BigDecimal.ZERO;
                BigDecimal gps_mach_cr_num_one_day = BigDecimal.ZERO ;
                BigDecimal man_local_num_one_day = BigDecimal.ZERO;

                for(DataRepairUserAlgorithmInfo info1:list){
                    if(info1.getGpsMachCountOneDay()!=null && !"".equals(info1.getGpsMachCountOneDay())){
                        gps_mach_count_one_day = gps_mach_count_one_day.add(new BigDecimal(info1.getGpsMachCountOneDay()));
                    }
                    if(info1.getGpsMachCrNumOneDay()!=null && !"".equals(info1.getGpsMachCrNumOneDay()))
                        gps_mach_cr_num_one_day = gps_mach_cr_num_one_day.add(new BigDecimal(info1.getGpsMachCrNumOneDay()));
                }
                String params = month+" "+day+" "+quhao+" "+gps_mach_count_one_day.doubleValue()+" "+gps_mach_cr_num_one_day.doubleValue()+" "+man_local_num_one_day;
                List<String> pyList = PythonUtil.callRepairUser(params);
                if(pyList!=null && pyList.size()>1){
                    spanAreaOrderTotal = pyList.get(0);
                    spanAreaPeopleTotal = pyList.get(1);
                }
            }
        }
        GetOneVO oneVO=new GetOneVO();
        // 随机生成1到10之间的整数
        /*int min = 1;
        int max = 10;
        int spanAreaOrderTotal = (int)(Math.random() * (max - min + 1)) + min;
        int spanAreaPeopleTotal = (int)(Math.random() * (max - min + 1)) + min;*/
        if(spanAreaOrderTotal.lastIndexOf(".") == spanAreaOrderTotal.length()-1)spanAreaOrderTotal = spanAreaOrderTotal.substring(0,spanAreaOrderTotal.length()-1);
        if(spanAreaPeopleTotal.lastIndexOf(".") == spanAreaPeopleTotal.length()-1)spanAreaPeopleTotal = spanAreaPeopleTotal.substring(0,spanAreaPeopleTotal.length()-1);
        oneVO.setSpanAreaOrderTotal(spanAreaOrderTotal);
        oneVO.setSpanAreaPeopleTotal(spanAreaPeopleTotal);
        vo.add(oneVO);
        return Result.suc(vo);
    }
}
